package com.dsbj.mp.icardservice.utils.dateprocess;

import com.zhixintech.common.DateUtil;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 时间日期处理（来自GIPSON-OCS系统）
 *
 * @author Caesar
 */
public class Caesar {
	public static final String DATETIME_YMDHMS = "yyyyMMddHHmmss";
	public static final String DATETIME_Y_M_D = "yyyy-MM-dd";
	public static final String DATETIME_YMD = "yyyyMMdd";
	public static final String DATETIME_CHINA_YMD = "yyyy年MM月dd日";
	public static final String DATETIME_CHINA_YMDHMS = "yyyy年MM月dd日  HH:mm:ss";
	public static final String DATETIME_Y_M_DHMS = "yyyy-MM-dd HH:mm:ss";
	public static final String DATETIME_DEFAULT = "yyyy/MM/dd";
	public static final String DATETIME_YM = "yyMM";
	
	public Caesar(String string) {
		// TODO Auto-generated constructor stub
	}
	
	public Caesar() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 通过输入日期字符串，获取星期
	 */
	public static String getWeek(String strTime) {
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf1.parse(strTime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
		String week = sdf.format(date);
		return week;
	}
	
	/**
	 * 根据给定时间参数获取指定格式的时间字符串
	 *
	 * @param date
	 * @param format
	 * @return
	 */
	public static String format(Date date, String format) {
		return new SimpleDateFormat((null == format || "".equals(format.trim())) ? DATETIME_DEFAULT : format).format(date);
	}
	
	/* 获取本年的结束时间 */
	public static Date getCurrYearLast() {
		Calendar currCal = Calendar.getInstance();
		int currentYear = currCal.get(Calendar.YEAR);
		return getYearLast(currentYear);
	}
	
	public static Date getYearLast(int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		calendar.roll(Calendar.DAY_OF_YEAR, -1);
		Date currYearLast = calendar.getTime();
		
		return currYearLast;
	}
	
	public static float compareDateOnDayTime(Date date1, Date date2) {
		long ss = date1.getTime() - date2.getTime();
		long day = 24 * 60 * 60 * 1000;
		return (float) ((double) ss / (double) day);
	}
	
	/**
	 * 获取指定年月的最后一天
	 *
	 * @param month
	 * @param format
	 * @return
	 */
	public static String getLastDayByMonth(String month, String format) {
		if (null != month && "" != month.trim()) {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
			try {
				Date date = df.parse(month);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(date);
				calendar.add(Calendar.MONTH, 1);
				calendar.setTimeInMillis(calendar.getTimeInMillis() - 1);
				return format(calendar.getTime(), format);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return month;
	}
	
	/**
	 * 获取本月的最后一天
	 *
	 * @param year  年
	 * @param month 月
	 * @return
	 */
	public static int lastDayOfMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		//设置年份
		cal.set(Calendar.YEAR, year);
		//设置月份
		cal.set(Calendar.MONTH, month - 1);
		//获取某月最大天数
		int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		//设置日历中月份的最大天数
		cal.set(Calendar.DAY_OF_MONTH, lastDay);
		//格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("dd");
		//本月的最后一天
		String lastDayOfMonth = sdf.format(cal.getTime());
		return Integer.parseInt(lastDayOfMonth);
	}
	
	/**
	 * 获取两个小时后的时间
	 *
	 * @param month
	 * @param format
	 * @return
	 */
	public static String getSetime(Date date) {
		long curren = date.getTime();
		curren -= 2 * 60 * 60 * 1000;
		Date da = new Date(curren);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return dateFormat.format(da);
	}
	
	public static String getp1(Map<String, Object> map1) {
		return null;
	}
	
	private ServletContext servletContext;
	
	public void fileDownload(HttpServletResponse response) {
		// 获取网站部署路径(通过ServletContext对象)，用于确定下载文件位置，从而实现下载
		String path = servletContext.getRealPath("/");
		
		// 1.设置文件ContentType类型，这样设置，会自动判断下载文件类型
		response.setContentType("multipart/form-data");
		// 2.设置文件头：最后一个参数是设置下载文件名(假如我们叫a.pdf)
		response.setHeader("Content-Disposition", "attachment;fileName=" + "a.pdf");
		ServletOutputStream out;
		// 通过文件路径获得File对象(假如此路径中有一个download.pdf文件)
		File file = new File(path + "download/" + "download.pdf");
		
		try {
			FileInputStream inputStream = new FileInputStream(file);
			
			// 3.通过response获取ServletOutputStream对象(out)
			out = response.getOutputStream();
			
			int b = 0;
			byte[] buffer = new byte[512];
			while (b != -1) {
				b = inputStream.read(buffer);
				// 4.写到输出流(out)中
				out.write(buffer, 0, b);
			}
			inputStream.close();
			out.close();
			out.flush();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}
	
	/* 比较两个日期直接相差的分钟数 */
	public static int compareDateOnMinute(Date date1, Date date2) {
		long ss = date1.getTime() - date2.getTime();
		long minute = 60 * 1000;
		return Integer.parseInt(ss / minute + "");
	}
	
	/* 比较两个日期直接相差的小时数 */
	public static float compareDateOnHour(Date date1, Date date2) {
		long ss = date1.getTime() - date2.getTime();
		long hour = 60 * 60 * 1000;
		return (float) ((double) ss / (double) hour);
	}
	
	public static byte[] parseHexStr2Byte(String hexStr) {
		if (hexStr.length() < 1)
			return null;
		byte[] result = new byte[hexStr.length() / 2];
		for (int i = 0; i < hexStr.length() / 2; i++) {
			int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
			int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
			result[i] = (byte) (high * 16 + low);
		}
		return result;
	}
	
	public static byte[] hexStr2BinArr(String hexString) {
		String hexStr = "0123456789ABCDEF";
		//hexString的长度对2取整，作为bytes的长度
		int len = hexString.length() / 2;
		byte[] bytes = new byte[len];
		byte high = 0;//字节高四位
		byte low = 0;//字节低四位
		for (int i = 0; i < len; i++) {
			//右移四位得到高位
			high = (byte) ((hexStr.indexOf(hexString.charAt(2 * i))) << 4);
			low = (byte) hexStr.indexOf(hexString.charAt(2 * i + 1));
			bytes[i] = (byte) (high | low);//高地位做或运算
		}
		return bytes;
	}
	
	/**
	 * 比较两个时间字符串的大小（yyyy-MM-dd)  包括等于
	 *
	 * @param endDate
	 * @param startDate
	 * @return endDate大返回true
	 * @throws ParseException
	 */
	public static Boolean compareDate(String endDate, String startDate) throws ParseException {
		Date endTime = DateUtil.parse(endDate, DateUtil.DATETIME_Y_M_D);
		Date startTime = DateUtil.parse(startDate, DateUtil.DATETIME_Y_M_D);
		long cha = endTime.getTime() - startTime.getTime();
		if (cha >= 0) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 比较两个时间字符串的大小（yyyy-MM-ddHMS)  包括等于
	 *
	 * @param endDate
	 * @param startDate
	 * @return endDate大返回true
	 * @throws ParseException
	 */
	public static Boolean compare(String endDate, String startDate) throws ParseException {
		Date endTime = DateUtil.parse(endDate, DateUtil.DATETIME_Y_M_DHMS);
		Date startTime = DateUtil.parse(startDate, DateUtil.DATETIME_Y_M_DHMS);
		long cha = endTime.getTime() - startTime.getTime();
		if (cha >= 0) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 比较两个时间字符串的大小（yyyy-MM-ddHMS)     不包括等于
	 *
	 * @param endDate
	 * @param startDate
	 * @return endDate大返回true
	 * @throws ParseException
	 */
	public static Boolean compareCut(String endDate, String startDate) {
		Date endTime;
		Date startTime;
		try {
			startTime = DateUtil.parse(startDate, DateUtil.DATETIME_Y_M_DHMS);
			endTime = DateUtil.parse(endDate, DateUtil.DATETIME_Y_M_DHMS);
			long cha = endTime.getTime() - startTime.getTime();
			if (cha > 0) {
				return true;
			} else {
				return false;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	
	/**
	 * 获取某个日期前后几天的日期
	 *
	 * @param str
	 * @param day
	 * @return
	 */
	public static String getDateAbout(String str, int day) {
		// 时间表示格式可以改变，yyyyMMdd需要写例如20160523这种形式的时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		// 将字符串的日期转为Date类型，ParsePosition(0)表示从第一个字符开始解析
		Date date = sdf.parse(str, new ParsePosition(0));
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// add方法中的第二个参数n中，正数表示该日期后n天，负数表示该日期的前n天
		calendar.add(Calendar.DATE, day);
		Date date1 = calendar.getTime();
		return sdf.format(date1);
	}
	
	/**
	 * 获取本周的第一天
	 *
	 * @return String
	 **/
	public static String getWeekStart(String str) {
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(str));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		//判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		cal.add(Calendar.WEEK_OF_MONTH, 0);
		cal.set(Calendar.DAY_OF_WEEK, 2);
		Date time = cal.getTime();
		return new SimpleDateFormat("yyyy-MM-dd").format(time);
	}
	
	/**
	 * 获取本周的最后一天
	 *
	 * @return String
	 **/
	public static String getWeekEnd(String str) {
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(str));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		//判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
		cal.add(Calendar.DAY_OF_WEEK, 1);
		Date time = cal.getTime();
		return new SimpleDateFormat("yyyy-MM-dd").format(time);
	}
	
	/**
	 * 获取随机日期时间
	 *
	 * @param beginDate 起始日期，格式为：yyyy-MM-dd HH:mm:ss
	 * @param endDate   结束日期，格式为：yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	
	public static String randomDate(String beginDate, String endDate) {
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date start = format.parse(beginDate);// 构造开始日期
			Date end = format.parse(endDate);// 构造结束日期
			// getTime()表示返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
			if (start.getTime() >= end.getTime()) {
				return null;
			}
			long date = random(start.getTime(), end.getTime());
			return format.format(new Date(date));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static long random(long begin, long end) {
		long rtn = begin + (long) (Math.random() * (end - begin));
		// 如果返回的是开始时间和结束时间，则递归调用本函数查找随机值
		if (rtn == begin || rtn == end) {
			return random(begin, end);
		}
		return rtn;
	}
	
	/**
	 * 设定一个日期时间，加几分钟（小时或者天）后得到新的日期
	 *
	 * @param day
	 * @param x
	 * @return
	 */
	public static String addDateMinut(String day, int x)//返回的是字符串型的时间，输入的是String day, int x
	{
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 24小时制
		//引号里面个格式也可以是 HH:mm:ss或者HH:mm等等，很随意的，不过在主函数调用时，要和输入的变
		//量day格式一致
		Date date = null;
		try {
			date = format.parse(day);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		if (date == null)
			return "";
		//System.out.println("front:" + format.format(date)); //显示输入的日期
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MINUTE, x);// 24小时制
		date = cal.getTime();
		//System.out.println("after:" + format.format(date));  //显示更新后的日期
		cal = null;
		return format.format(date);
		
	}
	
	/**
	 * 验证是否日期格式
	 *
	 * @param date yyyy/m/d
	 *             yyyy/m/dd
	 *             yyyy/mm/d
	 *             yyyy/mm/dd
	 *             yyyy-m-d
	 *             yyyy-m-dd
	 *             yyyy-mm-dd
	 *             yyyy-mm-d
	 *             yyyymmdd
	 *             mm/dd/yyyy
	 * @return
	 */
	public static Boolean checkDate(String date) {
		boolean success = true;
		Pattern p = Pattern.compile("((19|20)[0-9]{2}(-|/)(([1,3,5,7,8]|0[1,3,5,7,8]|1[0,2])(-|/)([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])|(0[4,6,9]|[4,6,9]|11)(-|/)([1-9]|0[1-9]|1[0-9]|2[0-9]|30)|((02|2)(-|/)([1-9]|0[1-9]|1[0-9]|2[0-9]))))|((19|20)[0-9]{2}((0[1,3,5,7,8]|1[0,2])(0[1-9]|1[0-9]|2[0-9]|3[0-1])|((0[4,6,9]|11)(0[1-9]|1[0-9]|2[0-9]|30))|((02)(0[1-9]|1[0-9]|2[0-9]))))|((0[1,3,5,7,8]|1[0,2])/(0[1-9]|1[0-9]|2[0-9]|3[0-1])|(0[4,6,9]|11)/(0[1-9]|1[0-9]|2[0-9]|30)|((02)/(0[1-9]|1[0-9]|2[0-9])))/(19|20)[0-9]{2}");
		Matcher matcher = p.matcher(date);
		boolean is_matches = matcher.matches();
		if (!is_matches) {
			success = false;
		}
		return success;
	}
	
	/**
	 * 日期格式转换(yyyy-mm-dd)
	 *
	 * @param date
	 * @return
	 */
	public static String convertDate(String date) {
		String returnDate = "";
		int index1 = date.indexOf("/");
		int index2 = date.indexOf("-");
		if (index1 >= 0) {
			if (index1 == 2) {
				String month = date.substring(0, index1);
				String dayAndYear = date.substring(index1 + 1);
				int index11 = dayAndYear.indexOf("/");
				String day = dayAndYear.substring(0, index11);
				String year = dayAndYear.substring(index11 + 1);
				returnDate = year + "-" + month + "-" + day;
			} else if (index1 == 4) {
				String year = date.substring(0, index1);
				String monthAndDay = date.substring(index1 + 1);
				int index11 = monthAndDay.indexOf("/");
				String month = convertString(Integer.parseInt((monthAndDay.substring(0, index11))));
				String day = convertString(Integer.parseInt(monthAndDay.substring(index11 + 1)));
				returnDate = year + "-" + month + "-" + day;
			}
		} else if (index2 >= 0) {
			if (index2 == 2) {
				String month = date.substring(0, index2);
				String dayAndYear = date.substring(index2 + 1);
				int index21 = dayAndYear.indexOf("-");
				String day = dayAndYear.substring(0, index21);
				String year = dayAndYear.substring(index21 + 1);
				returnDate = year + "-" + month + "-" + day;
			} else if (index2 == 4) {
				String year = date.substring(0, index2);
				String monthAndDay = date.substring(index2 + 1);
				int index21 = monthAndDay.indexOf("-");
				String month = convertString(Integer.parseInt((monthAndDay.substring(0, index21))));
				String day = convertString(Integer.parseInt(monthAndDay.substring(index21 + 1)));
				returnDate = year + "-" + month + "-" + day;
			}
		} else {
			// yyyymmdd
			String year = date.substring(0, 4);
			String month = date.substring(4, 6);
			String day = date.substring(6);
			returnDate = year + "-" + month + "-" + day;
		}
		return returnDate;
	}
	
	/**
	 * 金钱验证
	 *
	 * @param money
	 * @return
	 */
	public static boolean amountVerification(String money) {
		Pattern p = Pattern.compile("-?([0-9]*|[0-9]*([.]{1}[0-9]{1,2}))");
		Matcher matcher = p.matcher(money);
		boolean is_matches = matcher.matches();
		return is_matches;
	}
	
	public static String convertString(int num) {
		String num1 = "";
		switch (num) {
			case 1:
				num1 = "01";
				break;
			case 2:
				num1 = "02";
				break;
			case 3:
				num1 = "03";
				break;
			case 4:
				num1 = "04";
				break;
			case 5:
				num1 = "05";
				break;
			case 6:
				num1 = "06";
				break;
			case 7:
				num1 = "07";
				break;
			case 8:
				num1 = "08";
				break;
			case 9:
				num1 = "09";
				break;
			case 10:
				num1 = "10";
				break;
			case 11:
				num1 = "11";
				break;
			case 12:
				num1 = "12";
				break;
			case 13:
				num1 = "13";
				break;
			case 14:
				num1 = "14";
				break;
			case 15:
				num1 = "15";
				break;
			case 16:
				num1 = "16";
				break;
			case 17:
				num1 = "17";
				break;
			case 18:
				num1 = "18";
				break;
			case 19:
				num1 = "19";
				break;
			case 20:
				num1 = "20";
				break;
			case 21:
				num1 = "21";
				break;
			case 22:
				num1 = "22";
				break;
			case 23:
				num1 = "23";
				break;
			case 24:
				num1 = "24";
				break;
			case 25:
				num1 = "25";
				break;
			case 26:
				num1 = "26";
				break;
			case 27:
				num1 = "27";
				break;
			case 28:
				num1 = "28";
				break;
			case 29:
				num1 = "29";
				break;
			case 30:
				num1 = "30";
				break;
			case 31:
				num1 = "31";
				break;
		}
		return num1;
	}
	
	/**
	 * 判断是否是对应的格式的日期字符串  合集 常用字符串
	 * 带1个参数  1.时间字符串
	 * 仅仅供验证  不方便的的就是 有过有其他字符串必须要再次添加  非常不智能化
	 *
	 * @param dateStr
	 * @param datePattern
	 * @return
	 * @throws ParseException
	 */
	public static Date getDate(String str) throws ParseException {
		//最常用的 字符串 放在前面  一旦相匹配了  后面的字符串就不用比较了  提高运行效率
		boolean a = Lucifer.isValidDate(str, "yyyy-MM-dd HH:mm:ss");//yyyy-mm-dd
		if (a == false) {
			boolean b = Lucifer.isValidDate(str, "yyyy/MM/dd HH:mm:ss");//yyyy/mm/dd
			if (b == false) {
				boolean c = Lucifer.isValidDate(str, "yyyy-MM-dd HH:mm");//yyyy-mm-dd hh:MM
				if (c == false) {
					boolean d = Lucifer.isValidDate(str, "yyyy/MM/dd HH:mm");//yyyy/mm/dd hh:MM
					if (d == false) {
						boolean f = Lucifer.isValidDate(str, "yyyy年MM月dd日");//yyyy年mm月dd日
						if (f == false) {
							boolean g = Lucifer.isValidDate(str, "yyyy年MM月dd日 hh分mm秒");//yyyy年mm月dd日 hh分MM秒
							if (g == false) {
								boolean h = Lucifer.isValidDate(str, "yyyy/MM/dd");//yyyy年mm月dd日 hh分MM秒
								if (h == false) {
									boolean i = Lucifer.isValidDate(str, "yyyy-MM-dd");//yyyy年mm月dd日 hh分MM秒
									if (i == false) {
										return null;
									} else {
										return DateUtil.parse(str, "yyyy-MM-dd");
									}
								} else {
									return DateUtil.parse(str, "yyyy/MM/dd");
								}
							} else {
								return DateUtil.parse(str, "yyyy年MM月dd日 hh分mm秒");
							}
						} else {
							return DateUtil.parse(str, "yyyy年MM月dd日");
						}
					} else {
						return DateUtil.parse(str, "yyyy/MM/dd HH:mm");
					}
				} else {
					return DateUtil.parse(str, "yyyy-MM-dd HH:mm");
				}
			} else {
				return DateUtil.parse(str, "yyyy/MM/dd HH:mm:ss");
			}
		} else {
			return DateUtil.parse(str, "yyyy-MM-dd HH:mm:ss");
		}
	}
	
	public static String formatConversion(int num) {
		String str = "";
		switch (num) {
			case 1:
				str = "10000000";
				break;
			case 2:
				str = "01000000";
				break;
			case 3:
				str = "00100000";
				break;
			case 4:
				str = "00010000";
				break;
			case 5:
				str = "00001000";
				break;
			case 6:
				str = "00000100";
				break;
			case 7:
				str = "00000010";
				break;
			case 8:
				str = "00000001";
				break;
		}
		return str;
	}
	
	/**
	 * 获取五位数字符串
	 *
	 * @param str
	 * @return
	 */
	public static String getFivePlaceStr(String str) {
		int length = str.length();
		if (length == 1) {
			str = "0000" + str;
		} else if (length == 2) {
			str = "000" + str;
		} else if (length == 3) {
			str = "00" + str;
		} else if (length == 4) {
			str = "0" + str;
		}
		return str;
	}
	
	/**
	 * 两个时间之间相差距离多少天
	 *
	 * @param one 时间参数 1：
	 * @param two 时间参数 2：
	 * @return 相差天数
	 */
	public static long getDistanceDays(String str1, String str2) throws Exception {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date one;
		Date two;
		long days = 0;
		try {
			one = df.parse(str1);
			two = df.parse(str2);
			long time1 = one.getTime();
			long time2 = two.getTime();
			long diff;
			if (time1 < time2) {
				diff = time2 - time1;
			} else {
				diff = time1 - time2;
			}
			days = diff / (1000 * 60 * 60 * 24);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return days;
	}
	
	/**
	 * 比较给定两个时间之间相差天数  年假专用+1天
	 *
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int compareDateOnDayForNj(Date date1, Date date2) {
		long ss = date1.getTime() - date2.getTime();
		long day = 24 * 60 * 60 * 1000;
		return Integer.parseInt(ss / day + "") + 1;
	}
	/*public static void main(String[] args) throws ParseException   {
		  Date date = getDate("2012/12/12 12:12:12");
		  System.out.println(date);
		  try {
			getDistanceDays("2018-05-06","2018-05-31");
			System.out.println(getDistanceDays("2018-05-06","2018-06-06"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
	}*/
	
	public static boolean isValidDate(String str) {
		boolean convertSuccess = true;
		// 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
		try {
			// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
			format.setLenient(false);
			format.parse(str);
		} catch (ParseException e) {
			// e.printStackTrace();
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			convertSuccess = false;
		}
		return convertSuccess;
		
	}
	
	/**
	 * 字符串转日期
	 *
	 * @param stringDate
	 * @return
	 * @throws ParseException
	 */
	public static Date stringChangeDate(String stringDate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.parse(stringDate);
	}
	
}
